﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
namespace Z2.Core
{

	#region Using

	using System;
	using System.Collections.Generic;
	using Microsoft.Xna.Framework;
	using Microsoft.Xna.Framework.Graphics;

	#endregion

	/// <summary>
	/// Třída sloužící ke správě HUDU
	/// </summary>
	public class Hud
	{

		#region Consts

		private const string GRAPHICS_HEART = "heart";
		private const string GRAPHICS_LIVE = "heart";
		private const float PULSATE_MAX = 1.3f;
		private const float PULSATE_MIN = 0.7f;
		private const float PULSATE_SPEED = 0.1f;

		#endregion

		#region Members

		private List<Sprite> m_hearts;
		private Sprite m_live;
		private Vector2 m_livePosition;

		#endregion

		#region Constructor

		/// <summary>
		/// Konstruktor
		/// </summary>
		/// <param name="graphics"><see cref="GraphicsDeviceManager"/></param>
		public Hud(GraphicsDeviceManager graphics)
		{
			// Načteme sprite pro život
			this.m_live = SpriteManager.Instance.LoadSprite(Hud.GRAPHICS_LIVE, 1, 1);

			// Registrace událosstí
			World.Current.Player.LiveAdded += new EventHandler(this.Player_LiveAdded); // Hl. hráči přibylo srdíčko
			World.Current.Player.LiveRemoved += new EventHandler(this.Player_LiveRemoved); // Hl. hráči ubylo srdíčko

			// Načteme texturu do cache
			using (Sprite hearth = SpriteManager.Instance.LoadSprite(Hud.GRAPHICS_HEART, 1, 1)) 
			{
				this.m_livePosition = new Vector2(0, graphics.PreferredBackBufferHeight - hearth.FrameHeight);
				hearth.Dispose();
			}

			// Inicializace proměnné pro sprity srdíček
			this.m_hearts = new List<Sprite>();
			for (int i = 0; i < World.Current.Player.Hearts; ++i)
			{
				this.InitializeHeart();
			}
		}

		#endregion

		#region Methods

		/// <summary>
		/// Metoda načte a zinicializuje sprite srdíčka + nastaví počáteční animaci
		/// </summary>
		private void InitializeHeart()
		{
			Sprite heart = SpriteManager.Instance.LoadSprite(Hud.GRAPHICS_HEART, 1, 1); // Načteme srdíčko
			this.m_hearts.Add(heart); // Přidáme do kolekce
			heart.AnimationSpeed = 10; // Rychl. animace
			heart.Pulsate(Hud.PULSATE_SPEED, Hud.PULSATE_MAX, Hud.PULSATE_MIN, 5); // Nastavení animace "pulsace"
			heart.PulsateStop += new EventHandler(Heart_PulsateStop);
			heart.StartAnimation(); // Start animace
		}

		/// <summary>
		/// Metoda vykreslí HUD
		/// </summary>
		/// <param name="batch"><see cref="SpriteBatch"/></param>
		public void Draw(SpriteBatch batch)
		{
			foreach (Sprite heart in this.m_hearts)
			{
				int index = this.m_hearts.IndexOf(heart);
				heart.Draw(batch, Vector2.Add(this.m_livePosition, new Vector2(heart.FrameWidth * index, 0)));
			}
		}

		/// <summary>
		/// Metoda vrátí index posledního srdíčka, které smí být odebráno
		/// </summary>
		/// <returns>Index</returns>
		private int GetLast()
		{
			for (int i = this.m_hearts.Count - 1; i > -1; --i)
			{
				if (!this.m_hearts[i].IsRotating)
				{
					return i;
				}
			}
			return this.m_hearts.Count - 1;
		}

		#endregion

		#region Event Handling

		void Heart_PulsateStop(object sender, EventArgs e)
		{
			Sprite heart = (Sprite)sender;
			heart.PulsateStop -= new EventHandler(Heart_PulsateStop);
			heart.StopAnimation();
		}

		private void Player_LiveRemoved(object sender, EventArgs e)
		{
			if (this.m_hearts.Count == 0)
			{
				return;
			}
			int index = this.GetLast();

			Sprite heart = this.m_hearts[index];
			heart.Pulsate(0.05f, 0, 0, 1);
			heart.RotateTo(0.3f, (float)Math.PI * 2);
			heart.RotationStop += new EventHandler(Heart_RotationStop);

			heart.StartAnimation();
		}

		void Heart_RotationStop(object sender, EventArgs e)
		{
			Sprite sprite = ((Sprite)sender);
			sprite.StopAnimation();
			sprite.RotationStop -= new EventHandler(Heart_RotationStop);
			this.m_hearts.Remove(sprite);
		}

		private void Player_LiveAdded(object sender, EventArgs e)
		{
			this.InitializeHeart();
		}

		#endregion

	}
}
